package com.wizz.trade.auth.controller;

import com.wizz.trade.auth.dto.SysRoleDto;
import com.wizz.trade.auth.dto.page.SysRolePageDto;
import com.wizz.trade.auth.entity.SysRole;
import com.wizz.trade.auth.service.SysRoleService;
import com.wizz.trade.auth.service.SysUserService;
import com.wizz.trade.auth.vo.SysRoleVo;
import com.wizz.trade.common.ResponseResult;
import com.wizz.trade.common.constant.StatusCode;
import com.wizz.trade.common.PageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 权限模块/角色
 * @author xialinrui
 */
@RestController
@RequestMapping("/role")
@Slf4j
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;

    @Resource
    private SysUserService sysUserService;

    /**
     * 添加角色
     *
     * @param sysRoleDto 角色
     * @return ResponseResult
     */
    @PostMapping
    @PreAuthorize("hasAuthority('角色管理')")

    public ResponseResult<String> addsysRole(@RequestBody SysRoleDto sysRoleDto) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleDto, sysRole);
        sysRoleService.insert(sysRole);
        List<Long> menus = sysRoleDto.getMenus();
        if(!menus.isEmpty()){
            sysRoleService.bindMenu(sysRole.getId(), menus);
        }
        return new ResponseResult<>(StatusCode.OK, "添加成功");
    }

    /**
     * 更新角色信息
     *
     * @param sysRoleDto 角色
     * @param id         角色id
     * @return ResponseResult
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('角色管理')")

    public ResponseResult<String> updatesysRole(@RequestBody SysRoleDto sysRoleDto, @PathVariable("id") Long id) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleDto, sysRole);
        sysRole.setId(id);
        sysRoleService.update(sysRole);
        return new ResponseResult<>(StatusCode.OK, "修改成功");
    }

    /**
     * 删除角色
     *
     * @param id 角色id
     * @return ResponseResult
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('角色管理')")

    public ResponseResult<String> deleteSysRole(@PathVariable("id") Long id) {
        sysRoleService.delete(id);
        return new ResponseResult<>(StatusCode.OK, "删除成功");
    }

    /**
     * 查询角色
     *
     * @param sysRolePageDto 查询条件
     * @return 分页信息
     */
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('角色管理')")
    public ResponseResult<PageVo<SysRoleVo>> getPage(SysRolePageDto sysRolePageDto) {
        PageVo<SysRole> rawPage = sysRoleService.getPage(sysRolePageDto);
        List<SysRole> data = rawPage.getData();
        List<SysRoleVo> dataVo = new ArrayList<>();
        for (SysRole sysRole : data) {
            SysRoleVo sysRoleVo = new SysRoleVo();
            BeanUtils.copyProperties(sysRole, sysRoleVo);
            //设置拥有该角色的所有用户
            sysRoleVo.setUsers(sysUserService.getUserSimpleVoList(sysRoleVo.getId()));
            //设置该角色下的所有权限
            sysRoleVo.setMenus(sysRoleService.getMenusByRoleId(sysRoleVo.getId()));
            dataVo.add(sysRoleVo);
        }
        return new ResponseResult<>(StatusCode.OK, "查询成功",new PageVo<>(rawPage.getPages(), rawPage.getTotal(), dataVo));

    }

    /**
     * 禁用/启用
     *
     * @param id 角色id
     * @return ResponseResult
     */
    @PutMapping("/status/{id}")
    @PreAuthorize("hasAuthority('角色管理')")
    public ResponseResult<String> changeStatus(@PathVariable Long id) {
        sysRoleService.changeStatus(id);
        return new ResponseResult<>(StatusCode.OK, "状态更改成功");
    }

}
